package org.flower.dao.impl;

import org.flower.bean.Collect;
import org.flower.bean.CommodityDetails;
import org.flower.dao.CollectDao;
import org.flower.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CollectDaoImpl extends BaseDao implements CollectDao {
    @Override
    public List<Collect> getAll(int id) {
        String sql="select * from collect as c inner join `commoditydetails` as co on c.comDetailsId=co.id where c.userId=?";
        List<Collect> list = new ArrayList<Collect>();
        ResultSet rs =executeQuery(sql,id);
        try {
            while(rs.next()){
                CommodityDetails c = new CommodityDetails();
                c.setId( rs.getInt("co.id"));
                c.setComname(rs.getString("ComName"));
                c.setContent(rs.getString("Content"));
                c.setOutnum(rs.getInt("outNum"));
                c.setOutprice(rs.getDouble("outPrice"));
                c.setMarketprice(rs.getDouble("marketPrice"));
                c.setMaterialscience(rs.getString("MaterialScience"));
                c.setPacking(rs.getString("packing"));
                c.setDistribution( rs.getString("Distribution"));
                c.setComimage( rs.getString("comImage"));
                c.setDetailsimage(rs.getString("detailsImage"));
                c.setComshowid(rs.getInt("comShowId"));
                c.setClassid( rs.getInt("ClassId"));
                c.setStockd( rs.getInt("stockd"));
                Collect coll = new Collect(rs.getInt("c.id"),rs.getInt("c.comDetailsId"),rs.getInt("c.userId"));
                coll.setCommodityDetails(c);
                String ress[]=c.getComimage().split(",");
                c.setComimage(ress[0]);

                list.add(coll);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            try {
                closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return list;
    }

    @Override
    public boolean UpdateCollect( int userid,int collectId) {
        String sql="delete from `collect` where userId=? and ComdetailsId=?";
        return executeUpdate(sql,userid,collectId);
    }

    @Override
    public boolean getCollectExistByUidAndComId(int uid, int comid) {
        boolean exist = false;
        String sql = "select * from collect where userId=? and comDetailsId=?";
        ResultSet rs = executeQuery(sql,uid,comid);
        try {
            if(rs.next()){
                exist = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            try {
                closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return exist;
    }

    @Override
    public boolean addCollectByUidAndComId(int uid, int comid) {
        String sql = "insert into collect values(null,?,?)";
        return super.executeUpdate(sql,comid,uid);
    }

    @Override
    public boolean deleteCollectByUidAndComId(int uid, int comid) {
        String sql = "delete from collect where userId=? and comDetailsId=?";
        return super.executeUpdate(sql,uid,comid);
    }
}
